package com.chixing.service.impl;

import com.chixing.mapper.CarMapper;
import com.chixing.mapper.FavoriteMapper;
import com.chixing.pojo.Car;
import com.chixing.pojo.Favorite;
import com.chixing.pojo.FavoriteCarVO;
import com.chixing.pojo.FavoriteExample;
import com.chixing.service.FavoriteService;
import com.chixing.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 马博
 * * @date 2021/10/5
 */
@Service
public class FavoriteServiceImpl implements FavoriteService {
@Autowired
private FavoriteMapper favoriteMapper;
@Autowired
private CarMapper carMapper;
    @Override
    public ServerResponse getCustomerFavoriteCar(Integer custId) {
        List<FavoriteCarVO> favoriteCarVOS=new ArrayList<>();
        FavoriteExample favoriteExample = new FavoriteExample();
        favoriteExample.createCriteria().andCustIdEqualTo(custId).andStatusEqualTo(0);
        List<Favorite> favoriteList = favoriteMapper.selectByExample(favoriteExample);
        for (Favorite favorite:favoriteList){
            Car car=carMapper.selectByPrimaryKey(favorite.getCarId());
            favoriteCarVOS.add(new FavoriteCarVO(favorite,car));
        }
        if (favoriteCarVOS.size()!=0)
            return ServerResponse.getSuccess(favoriteCarVOS);
        return ServerResponse.getFail(null);
    }
}
